﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Xml;

// -----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace MenuGame2DXNA
{
    public class sOptions : Scene
    {
        private Texture2D sampleImage;
        private Game game;
        private KeyboardState oldState;
        private List<MyXNAButton> Options;
       // private List<Vector2> TempTopLeft;
        private int iSelected;
        List<bool> ListFlagSelect;
        public override void LoadContent(Game _game)
        {
            game = _game;
            // LoadMenu From Xml
            XmlDocument xdom = new XmlDocument();
            xdom.Load("Options.xml");

            XmlElement bg = (XmlElement)xdom.SelectSingleNode("Form/textures/texture");

            sampleImage = game.Content.Load<Texture2D>("Options/" + bg.InnerText);


            Options = new List<MyXNAButton>();
            ListFlagSelect = new List<bool>();
            ListFlagSelect.Add(false);
            ListFlagSelect.Add(true);
            ListFlagSelect.Add(false);
            XmlElement Buttons = (XmlElement)xdom.SelectSingleNode("Form/buttons");

            foreach (XmlElement button in Buttons.ChildNodes)
            {
                MyXNAButton tempButton = new MyXNAButton(game.Content, button.OuterXml,"Options/");
                //tempButton.
                Options.Add(tempButton);

            }
            iSelected = 0;
           

        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState kbState = Keyboard.GetState();
            if (kbState.IsKeyDown(Keys.Escape))
            {
                game.Scene = 0;
            }
            if (kbState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    if (iSelected < 3)
                        ListFlagSelect[iSelected] = !ListFlagSelect[iSelected];
                    else
                        CommandSelected();
                }

            }

            if (kbState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    SoundManager.Play("click");
                    if (iSelected < 3)
                        iSelected++;
                    else
                        iSelected = 0;

                }

            }

            // kbState.
            if (kbState.IsKeyDown(Keys.Up))
            {

                if (!oldState.IsKeyDown(Keys.Up))
                {
                    SoundManager.Play("click");
                    if (iSelected > 0)
                        iSelected--;
                    else
                        iSelected = 3;
                }

            }

            oldState = kbState;


            
            for (int i = 0; i < 4; i++)
            {
                Options[i].SetSprite(0);
                Options[i].Update(gameTime, Color.White);

            }
            if (iSelected >= 0)
                Options[iSelected].SetSprite(1);
            for (int i = 0; i < 3; i++)
            {
                if (ListFlagSelect[i])
                    Options[i + 4].SetSprite(1);
                else
                    Options[i + 4].SetSprite(0);
                Options[i+4].Update(gameTime, Color.White);

            }
        }
        private void CommandSelected()
        {
            SoundManager.Enable= ListFlagSelect[1];
            MusicManager.Enable = ListFlagSelect[0];

            game.Graphics.IsFullScreen = ListFlagSelect[2];
            game.Graphics.ApplyChanges();
            
            game.Scene = 0;
            
        }

        private bool GetSelectedIndex(Vector2 point)
        {
            for (int i = 0; i < Options.Count; i++)
            {
                if (Options[i].IsClicked(point))
                {
                    iSelected = i;

                    return true;
                }
            }
            return false;
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Rectangle destination = new Rectangle(
                            game.GraphicsDevice.Viewport.X, game.GraphicsDevice.Viewport.Y,
                            game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
            Rectangle source = new Rectangle(0, 0, sampleImage.Width, sampleImage.Height);
            spriteBatch.Draw(sampleImage, destination, source, Color.White);


            for (int i = 0; i < Options.Count; i++)
                Options[i].Draw(gameTime, spriteBatch);
        }

        public void Play()
        {
            
        }

        public void Stop()
        {
           
        }

        public void Pause()
        {
            
        }

        public void Resume()
        {
           
        }
    }
}
